package com.taobao.client.pages;

import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.buyer.SellerItemList;
import com.taobao.client.buyer.custom.CustomButton;
import com.taobao.client.sdk.AddressGetRequest;
import com.taobao.client.util.PopBox;

/**
 * Apply the singleton design pattern.
 * 
 * @author hujingfei.pt
 * 
 */
public class Bid extends Composite {

	private static Bid bid;
	private final static String RESULT_FLAG = "ResultCode";
	public final static String HISTORY_ITEM = "BidPage";
	private Frame f;
	private AsyncCallback<String> inputCallBack = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable arg0) {
			PopBox.error("系统异常", arg0.getMessage());
		}

		@Override
		public void onSuccess(String arg0) {
			if (f == null) {
				f = new Frame();
				f.setSize("800px", "600px");
				f.setUrl("buy.jsp?url=" + URL.encode(arg0));
				container.add(f);
			}
		}
	};

	// A vertical panel which contains various labels and textareas
	private VerticalPanel container = new VerticalPanel();
	private SellerItemList list;
//	private String iid;
	private List<CustomButton> buycars ;
	public AddressGetRequest input;

	native void init()/*-{	
		var widget = this; 
		
		$wnd.buy = function(tbAddressInfo,integer){
			if( !tbAddressInfo ){
//				$wnd.alert("in default ");
				widget.@com.taobao.client.pages.Bid::doBuy(Ljava/lang/String;)(integer);
			}
			else{
//				$wnd.alert("in buy");
				widget.@com.taobao.client.pages.Bid::doBuy(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;)(tbAddressInfo,integer);
			}
		}
	}-*/;

	native String getValue(JavaScriptObject tbAddressInfo, String key)/*-{
																		return tbAddressInfo[key];
																		}-*/;

	public void doBuy(String addressSize) {
		String province = "广东";
		String city = "广州";
		String area = "开发区";
		String name = "小马";
		String phone = "1354444";
		String mobile = "1234444";
		String division_code = "12345";
		String post_code = "510093";
		String address_detail = "广动路113号204房";
		
		String iid = "";
		String memos = "";
		String discount = "";
		
		
		for(CustomButton b : buycars){
			iid +=b.getIid()+",";
			memos +=b.getMemo()+",";
			discount += b.getDiscount()+",";
		}
		
		iid = iid.substring(0,iid.length()-1);
		memos = memos.substring(0,memos.length()-1);
		discount = discount.substring(0,discount.length()-1);
		
		boolean isNew = true;
		
		int size = Integer.parseInt(addressSize);
		if(size >= 10){
			isNew =false;
		}
		System.out.println("address = "+isNew +" length="+addressSize);
		
		list.addTrade(iid, name, phone, mobile, province, city, area,
				division_code, post_code, address_detail,isNew,memos,discount);
	}

	public void doBuy(JavaScriptObject tbAddressInfo,String addressSize) {
		
		
		String id = getValue(tbAddressInfo, "id");

		boolean isNew = false;
		if(id == null || "".equals(id) || "0".equals(id)){
			isNew= true;
		}
		
		
		int size = Integer.parseInt(addressSize);
		
		if(size >= 10){
			isNew =false;
		}
		System.out.println("address = "+isNew +" length="+addressSize);
			
		System.out.println("id="+id+" isNew = "+isNew);
		
		String province = getValue(tbAddressInfo, "province");
		String city = getValue(tbAddressInfo, "city");
		String area = getValue(tbAddressInfo, "area");
		String name = getValue(tbAddressInfo, "name");
		String phone = getValue(tbAddressInfo, "phone");
		String mobile = getValue(tbAddressInfo, "mobile");
		String division_code = getValue(tbAddressInfo, "division_code");
		String post_code = getValue(tbAddressInfo, "post_code");
		String address_detail = getValue(tbAddressInfo, "address_detail");
		
		String iid = "";
		String memos = "";
		String discount = "";
		for(CustomButton b : buycars){
			iid +=b.getIid()+",";
			memos +=b.getMemo()+",";
			discount += b.getDiscount()+",";
		}
		
		iid = iid.substring(0,iid.length()-1);
		
		list.addTrade(iid, name, phone, mobile, province, city, area,
				division_code, post_code, address_detail,isNew,memos,discount);
	}

	/**
	 * @param iid
	 *            the item id to set.
	 */
	public void setIid(String iid) {
	}

	public static Bid getBid(SellerItemList sellerItemList,
			List<CustomButton> buycar) {
		if (bid == null) {
			bid = new Bid();
			bid.list = sellerItemList;
		}
		bid.buycars = buycar;
		return bid;
	}

	/**
	 * The entry method.
	 */
	public static Bid getBid(SellerItemList sellerItemList, String iid) {
		if (bid == null) {
			bid = new Bid();
			bid.list = sellerItemList;
//			bid.iid = iid;
		}
		return bid;
	}

	/**
	 * Private constructor
	 */
	private Bid() {
		// JNSI method
		init();

		String queryString = Window.Location.getQueryString();

		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			// TODO Process the result.
		} else {
			initializeGUI();
		}
	}

	/**
	 * 
	 */
	private void initializeGUI() {
		container.add(createHead("我是买家"));

		// Initialize the input parameters
		input = new AddressGetRequest();
		input.initialize();
		// Get the address
		RPC.getService().getUrl(input.getURLMap(), inputCallBack);

		Button button = new Button("出价界面出不来请点击：默认参数创建订单");
		container.add(button);

		// Just for the testers. If the js page can't be displayed, the testers
		// could click 'button' to test the buy function
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				int mockAddressCount = 20;
				doBuy(mockAddressCount +"");
			}
		});

		initWidget(container);
		this.setSize("20px", "20px");
	}

	/**
	 * Create a head panel
	 * 
	 * @param title
	 *            the text to be displayed in the head panel
	 * @return a panel that is displayed in the head part always
	 */
	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}
}
